﻿using System;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using Team4Mock.Entity;
namespace Team4Mock.UserControl.Premise
{
    public partial class AddPremise : System.Web.UI.UserControl
    {
        PremiseEntity pe=new PremiseEntity();
        OrganisationEntity oe=new OrganisationEntity();
        private string err = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                {
                }
                else
                {
                    try
                    {
                        Label1.Text = "";
                        dlCountry.DataSource = oe.GetCountry();
                        dlCountry.DataBind();
                        GetCheckboxList(pe.GetAcc(), clAcc);
                        GetCheckboxList(pe.GetJcpOff(), clJcpOff);
                        GetCheckboxList(pe.GetLocDem(), clLocDemoIss);
                        GetCheckboxList(pe.GetLocHot(), clLocHot);
                        GetCheckboxList(pe.GetLocTyp(), clLocTyp);
                        GetCheckboxList(pe.GetOutLoc(), clOutLoc);
                        dlCateType.DataSource = pe.GetCatTyp();
                        dlCateType.DataBind();
                        
                    }
                    catch
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
                    }
                }
            }
        }
        
        string GetValueCheckBoxList(CheckBoxList cl)
        {
            string lb = string.Empty;
            for (int i = 0; i < cl.Items.Count; i++)
            {
                if (cl.Items[i].Selected == true)
                {
                    lb += cl.Items[i].Value + "=";
                }
            }
            return lb;
        }
        void GetCheckboxList(DataTable dt1, CheckBoxList cl)
        {
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                cl.Items.Add(new ListItem(dt1.Rows[i][1].ToString(), dt1.Rows[i][0].ToString()));
            }
        }
        protected void btnBack1_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FormView/Premise.aspx");
        }
        bool InsertPremise()
        {
            try
            {
                string idorg = idOrg.Value;
                string postcode = PostCode.Value;
                string town = Town.Value;
                string county = County.Value;
                string idmedia = idMediaContact.Value;
                string idcatering = idCatering.Value;
                string idhosting = idhostingcontact.Value;

                pe.LocationName = txtLocName.Text;
                pe.KnowAs = txtKnowAs.Text;
                pe.Organizationid = int.Parse(idorg);
                pe.LocationStatus = dlLocSta.SelectedValue;
                pe.LocationStatusDate = txtLocStaDate.Text;// DateTime.Parse(txtLocStaDate.Text);
                pe.AddressLine1 = txtAdd1.Text;
                pe.AddressLine2 = txtAdd2.Text;
                pe.AddressLine3 = txtAdd3.Text;
                pe.PostCode = postcode;
                pe.Town = town;
                pe.County = county;
                pe.Country = dlCountry.SelectedValue;
                pe.PrimaryLocation = ckPriLoc.Checked ? 1 : 0;
                pe.LocationManaged = ckLocMan.Checked ? 1 : 0;
                pe.STNetworkConectivity = ckSTNetCon.Checked ? 1 : 0;
                pe.LocationDescription = txtDesFull.Text;
                pe.PhonNumber = txtPhone.Text;
                pe.GeneralFaxNumber = txtGerNum.Text;
                pe.MinicommNumber = txtMiniNum.Text;
                pe.IsNewShop = ckIsNewShop.Checked ? 1 : 0;
                pe.ShopFlagDate = txtShopFlagDate.Text;// DateTime.Parse(txtShopFlagDate.Text);
                pe.SpecialistShop = ckSpecShop.Checked ? 1 : 0;
                pe.MediaContact = ckMediaContact.Checked ? 1 : 0;
                pe.MediaContactName = ckMediaContact.Checked ? idmedia : "";
                pe.LocalDemographicNotes = txtLocDemNotes.Text;
                pe.CateringFacilities = ckCateFac.Checked ? 1 : 0;
                pe.CateringContact = ckCateFac.Checked ? idcatering : "";
                pe.CateringType = dlCateType.SelectedValue;
                pe.ISNetwork = dlISNetwork.SelectedValue;
                pe.ClientITFacilities = ckClientIFFac.Checked ? 1 : 0;
                pe.ClientITFacilitiesDetail = ckClientIFFac.Checked ? txtClientFacDetails.Text : "";
                pe.RoomAvailability = ckRoomAvai.Checked ? 1 : 0;
                pe.VolunteeringOpportunities = ckVolunOpp.Checked ? 1 : 0;
                pe.TravelDetail = txtTravelDetail.Text;
                pe.TravelNearestBus = txtTravelBus.Text;
                pe.TravelNearestRail = txtTravelRail.Text;
                pe.TravelNearestAirport = txtTravelBus.Text;
                pe.HostVisits = ckHostVisit.Checked ? 1 : 0;
                pe.HostingContact = ckHostVisit.Checked ? idhosting : "";
                pe.VisitorParkingOnsite = ckVisitParkOnsite.Checked ? 1 : 0;
                pe.VisitorParkingSpaces = ckVisitParkOnsite.Checked ? txtVisitParkSpac.Text : "";
                pe.VisitorParkingAltemnative = txtVisitParkingAlt.Text;
                pe.RoomOnlyRate = txtRoomrate.Text;
                pe.BAndBRate = txtBBRate.Text;
                pe.DBBRate = txtDBBRate.Text;
                pe.DDRate = txtDDRate.Text;
                pe.HrRate = txtHrRate.Text;
                pe.TeaAndCoffe = txtTeaAndCoffee.Text;
                pe.Lunch = ckLunch.Checked ? 1 : 0;
                pe.NoOfMeetingRooms = txtNoMeetingRoom.Text;
                pe.MeetingRoomRatePerDay = txtMeetingRoomPerDay.Text;
                pe.Codings = dlCodings.SelectedValue;
                pe.NegotiatedRoomOnlyRate = txtNegoRoom.Text;
                pe.BBNegotiatedRate = txtBBNego.Text;
                pe.DBBNegotiatedRate = txtDBBRate.Text;
                pe.DDNegotiatedRate = txtDDNego.Text;
                pe.HrNegotiatedRate = txtHrNego.Text;
                pe.LastNegotiatedRate = txtLastNego.Text; //DateTime.Parse(txtLastNego.Text);
                pe.ReNegotiateOn = txtReNego.Text;//DateTime.Parse(txtReNego.Text);
                pe.PreferredStatus = dlPrefrred.SelectedValue;
                pe.Comments = txtComments.Text;
                pe.IsActive = ckactive.Checked ? 1 : 0;
                pe.Insert(pe);

                return true;
            }
            catch(Exception e)
            {
                err = e.Message;
                return false;
            }
        }
        void InsertAcc(int id)
        {
            pe.PremisesID = id;
            string a = GetValueCheckBoxList(clAcc);
            string[] tmp = a.Split('=');
            for (int i = 0; i < tmp.Count() - 1; i++)
            {
                pe.RefID = int.Parse(tmp[i]);
                pe.InsertAcc(pe);
            }
        }
        void InsertJcpOff(int id)
        {
            pe.PremisesID = id;
            string a = GetValueCheckBoxList(clJcpOff);
            string[] tmp = a.Split('=');
            for (int i = 0; i < tmp.Count() - 1; i++)
            {
                pe.RefID = int.Parse(tmp[i]);
                pe.InsertJcpOff(pe);
            }
        }
        void InsertLocDem(int id)
        {
            pe.PremisesID = id;
            string a = GetValueCheckBoxList(clLocDemoIss);
            string[] tmp = a.Split('=');
            for (int i = 0; i < tmp.Count() - 1; i++)
            {
                pe.RefID = int.Parse(tmp[i]);
                pe.InsertLocDem(pe);
            }
        }
        void InsertLocHot(int id)
        {
            pe.PremisesID = id;
            string a = GetValueCheckBoxList(clLocHot);
            string[] tmp = a.Split('=');
            for (int i = 0; i < tmp.Count() - 1; i++)
            {
                pe.RefID = int.Parse(tmp[i]);
                pe.InsertLocHot(pe);
            }
        }
        void InsertLocTyp(int id)
        {
            pe.PremisesID = id;
            string a = GetValueCheckBoxList(clLocTyp);
            string[] tmp = a.Split('=');
            for (int i = 0; i < tmp.Count() - 1; i++)
            {
                pe.RefID = int.Parse(tmp[i]);
                pe.InsertLocTyp(pe);
            }
        }
        void InsertOutLoc(int id)
        {
            pe.PremisesID = id;
            string a = GetValueCheckBoxList(clOutLoc);
            string[] tmp = a.Split('=');
            for (int i = 0; i < tmp.Count() - 1; i++)
            {
                pe.RefID = int.Parse(tmp[i]);
                pe.InsertOutLoc(pe);
            }
        }
        
        protected void btnCreate_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                //Amend
               
            }
            else
            {
                Label1.Text = "";
                if (txtPhone.Text == "")
                {
                    Label1.Text += "Input Phone Number" + "</br>";
                }
                if (idOrg.Value == "")
                {
                    Label1.Text += "Select Organization";
                }
                if (txtLocName.Text == "")
                {
                    Label1.Text += "Input Location Name" + "</br>";
                }
                if (dlLocSta.SelectedIndex == 0)
                {
                    Label1.Text += "Select Location Status" + "</br>";
                }
                if (PostCode.Value == "")
                {
                    Label1.Text += "Select Post Code" + "</br>";
                }
                if (ckMediaContact.Checked == true)
                {
                    if (idMediaContact.Value == "")
                    {
                        Label1.Text += "Select Media Contact" + "</br>";
                    }
                }
                if (ckCateFac.Checked == true)
                {
                    if (idCatering.Value == "")
                    {
                        Label1.Text += "Select Catering Contact" + "</br>";
                    }
                }
                if (ckHostVisit.Checked == true)
                {
                    if (idhostingcontact.Value == "")
                    {
                        Label1.Text += "Select Hosting Contact" + "</br>";
                    }
                }
                //neu da nhap het
                if (Label1.Text == "")
                {

                    if (InsertPremise() == true)
                    {
                        try
                        {
                            int id = pe.GetID();
                            InsertAcc(id);
                            InsertJcpOff(id);
                            InsertLocDem(id);
                            InsertLocHot(id);
                            InsertLocTyp(id);
                            InsertOutLoc(id);
                            Label1.Text = "Insert Success";
                        }
                        catch
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
                        }
                    }
                    else
                    {
                        Label1.Text = err; // "Insert Premise failure";
                    }
                }
            }
        }
    }
}